package com.finance.system.support.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.finance.system.user.constant.UserConstant;
import com.finance.system.support.exception.FinanceBusinessException;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class JSONUtils {
	private final static ObjectMapper objectMapper = new ObjectMapper();
	private static final Logger log = Logger.getLogger(JSONUtils.class);

	private JSONUtils() {

	}

	public static ObjectMapper getInstance() {

		return objectMapper;
	}

	/**
	 * javaBean,list,array convert to json string
	 */
	public static String obj2json(Object obj) {
		try {
			return objectMapper.writeValueAsString(obj);
		} catch (JsonProcessingException e) {
			log.error(UserConstant.JSON_CONVERT_ERROR,e);
			throw new FinanceBusinessException(UserConstant.JSON_CONVERT_ERROR);
		}
	}

	/**
	 * json string convert to javaBean
	 */
	public static <T> T json2Object(String jsonStr, Class<T> clazz) {
		try {
			return objectMapper.readValue(jsonStr, clazz);
		} catch (Exception e) {
			log.error(UserConstant.JSON_CONVERT_ERROR,e);
			throw new FinanceBusinessException(UserConstant.JSON_CONVERT_ERROR);
		}
	}

	/**
	 * json string convert to map
	 */
	@SuppressWarnings("unchecked")
	public static <T> Map<String, Object> json2map(String jsonStr) {
		try {
			return objectMapper.readValue(jsonStr, Map.class);
		} catch (Exception e) {
			log.error(UserConstant.JSON_CONVERT_ERROR,e);
			throw new FinanceBusinessException(UserConstant.JSON_CONVERT_ERROR);
		}
	}

	/**
	 * json string convert to map with javaBean
	 */
	public static <T> Map<String, T> json2map(String jsonStr, Class<T> clazz) {
		try {
			Map<String, Map<String, Object>> map = objectMapper.readValue(jsonStr, new TypeReference<Map<String, T>>() {
			});
			Map<String, T> result = new HashMap<String, T>();
			for (Entry<String, Map<String, Object>> entry : map.entrySet()) {
				result.put(entry.getKey(), map2pojo(entry.getValue(), clazz));
			}
			return result;
		} catch (Exception e) {
			log.error(UserConstant.JSON_CONVERT_ERROR,e);
			throw new FinanceBusinessException(UserConstant.JSON_CONVERT_ERROR);
		}
	}

	/**
	 * json array string convert to list with javaBean
	 */
	public static <T> List<T> json2list(String jsonArrayStr, Class<T> clazz) {
		try {
			List<Map<String, Object>> list = objectMapper.readValue(jsonArrayStr, new TypeReference<List<T>>() {
			});
			List<T> result = new ArrayList<T>();
			for (Map<String, Object> map : list) {
				result.add(map2pojo(map, clazz));
			}
			return result;
		} catch (Exception e) {
			log.error(UserConstant.JSON_CONVERT_ERROR,e);
			throw new FinanceBusinessException(UserConstant.JSON_CONVERT_ERROR);
		}
	}

	/**
	 * map convert to javaBean
	 */
	public static <T> T map2pojo(Map<?, ?> map, Class<T> clazz) {
		return objectMapper.convertValue(map, clazz);
	}

}
